﻿using AutoMapper;
using BPMN.Application.Contracts.DTO.Request.Brands;
using BPMN.Application.Contracts.DTO.Request.RepairOrders;
using BPMN.Application.Contracts.DTO.Request.Role;
using BPMN.Application.Contracts.DTO.Response;
using BPMN.Application.Contracts.DTO.Response.Brands;
using BPMN.Application.Contracts.DTO.Response.Role;
using BPMN.Application.Contracts.DTO.Response.RolePermission;
using BPMN.Application.Contracts.DTO.Response.UserInfo;
using BPMN.Application.IService;
using BPMN.Domain.Entity.EmployeePerformance;
using BPMN.Domain.Entity.RepairOrders;
using BPMN.Domain.Entity.User;
using BPMN.Domain.Interface;
using BPMN.DomianShare;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace BPMN.Application.Service
{
    public class RepairOrdersAppService : IRepairOrdersAppService
    {
        private readonly IRepositoryBase<RepairOrders> RepairOrders;
        private readonly IRepositoryBase<Equipment> Equipment;
        private readonly IRepositoryBase<Brands> Brands;
        private readonly IMapper mapper;

        public RepairOrdersAppService(IRepositoryBase<RepairOrders> RepairOrders, IRepositoryBase<Equipment> Equipment, IRepositoryBase<Brands> Brands, IMapper mapper)
        {
            this.RepairOrders = RepairOrders;
            this.Equipment = Equipment;
            this.Brands = Brands;
            this.mapper = mapper;
        }


        /// <summary>
        /// 维修单显示
        /// </summary>
        /// <returns></returns>
        public async Task<ResultResponseDto<List<RepairOrdersRequestDto>>> GetRepairOrders(string? RName,string? Rremark,string? EName)
        {
            var list =  (from a in RepairOrders.GetAll().Result.ToList()
                        join b in Equipment.GetAll().Result.ToList()
                        on  a.Equipment_Id equals b.Eid
                       join c in Brands.GetAll().Result.ToList()
                       on a.Brand_Id equals c.Bid
                         select new RepairOrdersRequestDto
                       {
                             Rid=a.Rid,
                             RName=a.RName,
                             Rnum=a.Rnum,
                             AddTime =a.AddTime,
                             Rremark=a.Rremark,
                             Eid = b.Eid,
                             EName = b.EName,
                             Enum = b.EName,
                             EType = b.EType,
                             Ecare = b.Ecare,
                             Bid = c.Bid,
                             BName=c.BName,
                             CreatorId=a.CreatorId,
                             CreateDate=a.CreateDate,
                             Updator = a.Updator,
                             UpdateDate = a.UpdateDate,

                         }).ToList();

            if (!string.IsNullOrEmpty(RName))
            {
                list = list.Where(x => x.RName.Contains(RName)).ToList();
            }
            if (!string.IsNullOrEmpty(Rremark))
            {
                list = list.Where(x => x.Rremark.Contains(Rremark)).ToList();
            }
            if (!string.IsNullOrEmpty(EName))
            {
                list = list.Where(x => x.EName.Contains(EName)).ToList();
            }


            return new ResultResponseDto<List<RepairOrdersRequestDto>>()
            {
                Code = ResultCode.Ok,
                ErrorInfo = "获取成功",
                Data = mapper.Map<List<RepairOrdersRequestDto>>(list)
            };

        }


        /// <summary>
        /// 维修单单独显示
        /// </summary>
        /// <returns></returns>
        public async Task<ResultResponseDto<List<RepairOrdersRequestDto>>> GetRepairs()
        {
            var list = (from a in RepairOrders.GetAll().Result.ToList()
                        join b in Equipment.GetAll().Result.ToList()
                        on a.Equipment_Id equals b.Eid
                        join c in Brands.GetAll().Result.ToList()
                        on a.Brand_Id equals c.Bid
                        select new RepairOrdersRequestDto
                        {
                            Rid = a.Rid,
                            RName = a.RName,
                            Rnum = a.Rnum,
                            AddTime= a.AddTime,
                            Rremark = a.Rremark,
                            Eid = b.Eid,
                            EName = b.EName,
                            Enum = b.EName,
                            EType = b.EType,
                            Ecare = b.Ecare,
                            Bid = c.Bid,
                            BName = c.BName,
                            CreatorId = a.CreatorId,
                            CreateDate = a.CreateDate,
                            Updator = a.Updator,
                            UpdateDate = a.UpdateDate,

                        }).ToList();


            return new ResultResponseDto<List<RepairOrdersRequestDto>>()
            {
                Code = ResultCode.Ok,
                ErrorInfo = "获取成功",
                Data = mapper.Map<List<RepairOrdersRequestDto>>(list)
            };

        }



        /// <summary>
        ///  维修单添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<ResultResponseDto<int>> AddRepairOrders(RepairOrdersRequestDto entity)
        {
            var slist = RepairOrders.GetAll().Result.Where(x=>x.RName== entity.RName).FirstOrDefault();
            if (slist != null)
            {
                return new ResultResponseDto<int>(ResultCode.Fail, "维修单名称已存在");
            }

            var obj = mapper.Map<RepairOrders>(entity);
            int n = await RepairOrders.Add(obj);
            if (n > 0)
            {
                return new ResultResponseDto<int>();
            }
            else
            {
                return new ResultResponseDto<int>(ResultCode.Fail, "添加失败");
            }
        }

        /// <summary>
        /// 根据Id获取角色信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<ResultResponseDto<RepairOrdersRequestDto>> GetRepairOrdersById(int Rid)
        {
            var slist = await RepairOrders.Get(Rid);

            return new ResultResponseDto<RepairOrdersRequestDto>()
            {
                Code = ResultCode.Ok,
                ErrorInfo = "获取成功",
                Data = mapper.Map<RepairOrdersRequestDto>(slist)
            };

        }
        /// <summary>
        /// 修改功能
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<ResultResponseDto<int>> UpdateRepairOrders(RepairOrdersRequestDto entity)
        {
            var slist = await (await RepairOrders.GetAll()).FirstOrDefaultAsync();

            var obj = mapper.Map<RepairOrders>(entity);
            int n = await RepairOrders.Update(obj);
            if (n > 0)
            {
                return new ResultResponseDto<int>();
            }
            else
            {
                return new ResultResponseDto<int>(ResultCode.Fail, "添加失败");
            }
        }

        /// <summary>
        /// 品牌信息
        /// </summary>
        /// <returns></returns>
        public async Task<ResultResponseDto<List<Brands>>> GetBrandsList()
        {
            var slist =  Brands.GetAll().Result.ToList();

            return new ResultResponseDto<List<Brands>>()
            {
                Code = ResultCode.Ok,
                ErrorInfo = "获取成功",
                Data = slist
            };
        }


    }
}
